const https = require('https');
const cheerio = require('cheerio');

// 要获取的网页 URL
const url = 'https://www.baidu.com';

https.get(url, (res) => {
  let data = '';

  // 接收数据
  res.on('data', (chunk) => {
    data += chunk;
  });

  // 请求结束后处理数据
  res.on('end', () => {
    console.log(data);
    const $ = cheerio.load(data);

    // 提取所有图片地址
    const imgUrls = [];
    $('img').each((index, element) => {
      const imgUrl = $(element).attr('src');
      imgUrls.push(imgUrl);
    });

    // 打印所有图片地址
    console.log('图片地址:');
    imgUrls.forEach(imgUrl => {
      console.log(imgUrl);
    });
  });

}).on('error', (err) => {
  console.error(`请求错误: ${err.message}`);
});

